﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace 小次郎点餐系统
{
    public partial class forgetForm : Form
    {
        public forgetForm()
        {
            InitializeComponent();
        }
        
        private void forgetForm_Load(object sender, EventArgs e)
        {
            this.txt_yz.Visible = false;
        }
        private void btn_ok_Click(object sender, EventArgs e)
        {
            int type = 0;
            string phone = this.txt_forget_phone.Text.Trim();
            string new_pwd = this.txt_forget_new.Text.Trim();
                //根据电话号码去查询数据库
                string sql = "";
                if (type == (int)usertype.user)
                {
                    sql = string.Format("select * from [user] where phone='{0}'", phone);
                }
                else
                {
                    sql = string.Format("select * from manager where phone='{0}'", phone);
                }
                DataTable table = DBHelper.read(sql);
                if (table.Rows.Count > 0)
                {
                    //如果查询出来了就可以修改密码
                    string xg_sql = "";
                    if (type == (int)usertype.user)
                    {
                        xg_sql = string.Format("update [user] set password='{0}' where phone='{1}'", new_pwd, phone);
                    }
                    else
                    {
                        xg_sql = string.Format("update manager set password='{0}' where phone='{1}'", new_pwd, phone);
                    }
                    int rs = DBHelper.write(xg_sql);
                    if (rs > 0)
                    {
                        MessageBox.Show("更换密码成功");
                    }
                    else
                    {
                        MessageBox.Show("更换密码失败..");
                    }
                }
            }

        private void txt_forget_yz_Click(object sender, EventArgs e)
        {
            //如果验证码文本框点击了就显示
            this.txt_yz.Visible = true;
            //生成随机数
            Random number = new Random();
            string num = number.Next(0, 10000).ToString();
            this.txt_yz.Text = num;
            if (this.txt_forget_yz.Text!=num)
            {
                MessageBox.Show("验证码输入不正确");
            }
        }

        private void txt_yz_Click(object sender, EventArgs e)
        {
            Random number = new Random();
            string num = number.Next(0, 10000).ToString();
            this.txt_yz.Text = num;
        }
    }
}
